package com.itcast.service.impl;

import com.itcast.dao.AdminDao;
import com.itcast.domain.Admin;
import com.itcast.service.AdminService;
import com.itcast.utils.ResponseBodyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("adminService")
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminDao adminDao;

    /**
     * 注册
     * @param admin
     * @return
     * @throws Exception
     */
    @Override
    public String register(Admin admin) throws Exception {
        if (admin.getAdmin() != null && admin.getPassword() != null && admin.getTel() != null) {
            int count = adminDao.register(admin);
            if (count > 0){
                return ResponseBodyUtil.getSuccessNoObject("注册");
            } else {
                return ResponseBodyUtil.getUnSuccessNoObject("注册");
            }
        }else {
            return ResponseBodyUtil.getUnSuccessNoObject("注册");
        }
    }

    /**
     * 登录
     * @param admin
     * @return
     */
    @Override
    public String login(Admin admin) throws Exception {
        if (admin.getAdmin() != null && admin.getPassword() != null) {
            System.out.println(admin);
            Boolean count = adminDao.login(admin);
            System.out.println(count);
            if (count != null){
                return ResponseBodyUtil.getSuccessNoObject("登录");
            } else {
                return ResponseBodyUtil.getUnSuccessNoObject("登录");
            }
        }else {
            return ResponseBodyUtil.getUnSuccessNoObject("登录");
        }
    }

    /**
     * 查询所有管理员
     * @return
     */
    @Override
    public List<Admin> findAll() {
        return adminDao.findAll();
    }

    /**
     * 通过id删除管理员
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public String deleteAdmin(String id) throws Exception {
        if (id != null) {
            int count = adminDao.deleteAdmin(Integer.valueOf(id));
            if (count > 0) {
                return ResponseBodyUtil.getSuccessNoObject("删除");
            } else {
                return ResponseBodyUtil.getUnSuccessNoObject("删除");
            }
        } else {
            return ResponseBodyUtil.getUnSuccessNoObject("删除");
        }
    }
}
